<template>
  <div class="sms-pluginSmsConfig-edit">
    <!-- 添加或修改短信配置对话框 -->
    <el-dialog v-model="isShowDialog" width="800px" :close-on-click-modal="false" :destroy-on-close="true">
      <template #header>
        <div v-drag="['.sms-pluginSmsConfig-edit .el-dialog', '.sms-pluginSmsConfig-edit .el-dialog__header']">{{(!formData.id || formData.id==0?'添加':'修改')+'短信配置'}}</div>
      </template>
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="短信平台" prop="smsType">
          <el-input v-model="formData.smsType" placeholder="请输入短信平台" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formData.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="配置" prop="config">
          <el-input v-model="formData.config" type="textarea" :rows="20" placeholder="请输入配置" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="onSubmit" :disabled="loading">确 定</el-button>
          <el-button @click="onCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { reactive, toRefs, defineComponent,ref,unref,getCurrentInstance } from 'vue';
import {ElMessageBox, ElMessage, FormInstance,UploadProps} from 'element-plus';
import {
  listPluginSmsConfig,
  getPluginSmsConfig,
  delPluginSmsConfig,
  addPluginSmsConfig,
  updatePluginSmsConfig,
} from "/@/api/system/sms/pluginSmsConfig";
import {
  PluginSmsConfigTableColumns,
  PluginSmsConfigInfoData,
  PluginSmsConfigTableDataState,
  PluginSmsConfigEditState
} from "/@/views/plugins/sms/pluginSmsConfig/list/component/model"
export default defineComponent({
  name:"ApiV1PluginsSmsPluginSmsConfigEdit",
  components:{
  },
  props:{
    statusOptions:{
      type:Array,
      default:()=>[]
    },
  },
  setup(props,{emit}) {
    const {proxy} = <any>getCurrentInstance()
    const formRef = ref<HTMLElement | null>(null);
    const menuRef = ref();
    const state = reactive<PluginSmsConfigEditState>({
      loading:false,
      isShowDialog: false,
      formData: {
        id: undefined,
        smsType: undefined,
        remark: undefined,
        status: false ,
        config: undefined,
      },
      // 表单校验
      rules: {
        id : [
            { required: true, message: "ID不能为空", trigger: "blur" }
        ],
        smsType : [
            { required: true, message: "短信平台不能为空", trigger: "blur" }
        ],
        remark : [
            { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        status : [
            { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        config : [
            { required: true, message: "配置不能为空", trigger: "blur" }
        ],
      }
    });
    // 打开弹窗
    const openDialog = (row?: PluginSmsConfigInfoData) => {
      resetForm();
      if(row) {
        getPluginSmsConfig(row.id!).then((res:any)=>{
          const data = res.data;
          data.status = ''+data.status
          state.formData = data;
      })
    }
      state.isShowDialog = true;
    };
    // 关闭弹窗
    const closeDialog = () => {
      state.isShowDialog = false;
    };
    // 取消
    const onCancel = () => {
      closeDialog();
    };
    // 提交
    const onSubmit = () => {
      const formWrap = unref(formRef) as any;
      if (!formWrap) return;
      formWrap.validate((valid: boolean) => {
        if (valid) {
          state.loading = true;
          if(!state.formData.id || state.formData.id===0){
            //添加
          addPluginSmsConfig(state.formData).then(()=>{
              ElMessage.success('添加成功');
              closeDialog(); // 关闭弹窗
              emit('pluginSmsConfigList')
            }).finally(()=>{
              state.loading = false;
            })
          }else{
            //修改
          updatePluginSmsConfig(state.formData).then(()=>{
              ElMessage.success('修改成功');
              closeDialog(); // 关闭弹窗
              emit('pluginSmsConfigList')
            }).finally(()=>{
              state.loading = false;
            })
          }
        }
      });
    };
    const resetForm = ()=>{
      state.formData = {
        id: undefined,
        smsType: undefined,
        remark: undefined,
        status: false ,
        config: undefined,
      }
    };
    return {
      proxy,
      openDialog,
      closeDialog,
      onCancel,
      onSubmit,
      menuRef,
      formRef,
      ...toRefs(state),
    };
  }
})
</script>
<style scoped>
  .kv-label{margin-bottom: 15px;font-size: 14px;}
  .mini-btn i.el-icon{margin: unset;}
  .kv-row{margin-bottom: 12px;}
</style>
